package com.pjj.userservice.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pjj.base.base.utils.MathUtil;
import com.pjj.base.base.utils.ResponseUtil;
import com.pjj.userservice.mapper.UserMapper;
import com.pjj.userservice.pojo.User;
import com.pjj.userservice.utils.TokenUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Wrapper;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 76004
 * @apiNote
 * @date 2023/2/11 11:38
 */
@Service
public class UserService {

  @Autowired
  private UserMapper userMapper;

  public Object selectById(String id){
    Map<String,Object>map = new HashMap<>();
    map.put("code",100);
    map.put("msg","查询成功");
    map.put("data",userMapper.selectById(id));
    return map;
  }

  public Object selectPage(Integer pageNum, Integer pageSize){
    Map<String,Object>map = new HashMap<>();
    map.put("code",100);
    map.put("msg","查询成功");
    Page<User> page = new Page<>(pageNum,pageSize);
    map.put("data",userMapper.selectPage(page,null).getRecords());
    return map;
  }

  public Object deleteById(String id){
    Map<String,Object>map = new HashMap<>();
    userMapper.deleteById(id);
    map.put("code",100);
    map.put("msg","删除成功");
    return map;
  }

  public Object updateUser(User user){
    Map<String,Object>map = new HashMap<>();
    map.put("code",100);
    map.put("msg","更新成功");
    userMapper.updateById(user);
    return map;
  }

  public Object insertUser(User user){
    Map<String,Object>map = new HashMap<>();
    map.put("code",100);
    map.put("msg","添加成功");
    userMapper.insert(user);
    return map;
  }


  public Object userLogin(String id,String password){
    Map<String,Object> map = new HashMap<>();
    QueryWrapper query = new QueryWrapper<>();
    query.eq("id",id);
    int num = userMapper.selectCount(query);
    if(num != 0){
      User user = userMapper.selectById(id);
      if(password.equals(user.getPassword())) {
        String token = TokenUtil.generateToken(user);
        System.out.println("user ---> " + id + " login success!!!");
        map.put("token", token);
        map.put("code", 100);
        map.put("msg", id + "登录成功");
        return map;
      }
    }
    System.out.println("user ---> "+id + "login failed!!!");
    map.put("code",200);
    map.put("msg",id+"登陆失败: 用户名或密码错误");
    return map;
  }

  public Object userRegister(String id,String password){
    Map<String,Object> map = new HashMap<>();
    QueryWrapper query = new QueryWrapper<>();
    query.eq("id",id);
    int num = userMapper.selectCount(query);
    if(num != 0){
      map.put("code",200);
      map.put("msg","用户"+id+"已经存在!");
    }
    else{
      User user = new User();
      user.setId(id);
      user.setPassword(password);
      user.setAuthority(10);
      user.setGender(0);
      userMapper.insert(user);
      map.put("code",100);
      map.put("msg","用户: "+id+"注册成功!");
    }
    return map;
  }

  public Map<String,Object> selectCountById(String userId){
    return ResponseUtil.success(userMapper.selectCount(new QueryWrapper<User>().eq("id",userId)));
  }

  public Map<String,Object> selectByAuthority(Integer rank,Integer pageNum,Integer pageSize){
    System.out.println(new QueryWrapper<User>().eq("authority", MathUtil.quickPow(2,rank))
            .or().eq("authority",MathUtil.quickPow(3,rank))
            .or().eq("authority",MathUtil.quickPow(5,rank)).getSqlSelect());
    return ResponseUtil.success(userMapper.selectPage(new Page<User>(pageNum,pageSize),
            new QueryWrapper<User>().eq("authority", MathUtil.quickPow(2,rank))
                    .or().eq("authority",MathUtil.quickPow(3,rank))
                    .or().eq("authority",MathUtil.quickPow(5,rank))).getRecords());

  }


}
